#   Copyright (C) 2008      Tri Tech Information Systems Inc.
# 
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
# 
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
# 
#   You should have received a copy of the GNU General Public License along
#   with this program; if not, write to the Free Software Foundation, Inc.,
#   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#   
from callable import CallableWrapper
from formatters import generator, formatter, argument_list

class ConstructorWrapper(CallableWrapper):
        
    @generator
    @formatter( argument_list )
    def generate_defSpec( self ):
        keyargs = self.use_keywords and '(%(keyargs)s)' or ''
        docstring = self.docstring if self.docstring else ''
        if keyargs and docstring:
            the_comma = ','
        else:
            the_comma = ''
        return ["bpy::init< %(argumentList)s >( " + docstring + the_comma +
            keyargs + ")"]

class InterfaceConstructorWrapper( ConstructorWrapper ):
    TEMPLATE_OVERLOAD = '''
    Wrap_%(parent.name)s(PyObject * pyobj %(comma_multi_param)s %(params)s) :
        %(parent.qualifiedName)s(%(param_values)s)
    {
        bpy::detail::initialize_wrapper(pyobj, this);
        __init_wrapper();
    }
    
    static void __construct(PyObject * pyobj %(comma_multi_param)s %(params)s)
    {
        new Wrap_%(parent.name)s(pyobj %(comma_multi_param)s %(param_values)s);
    }
    '''

    def generate_comma_multi_param(self):
        if len(self.item.arguments):
            return ','
        else:
            return ''

    @generator
    @formatter( argument_list )
    def generate_defSpec( self ):
        keyargs = self.use_keywords and '(%(keyargs)s)' or ''
        return ['"__init__"', "( void (*)(PyObject * %(comma_multi_param)s %(argumentList)s))&Wrap_%(name)s::__construct",self.docstring, keyargs]


